package com.blunka.framework.client.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.tree.DefaultMutableTreeNode;

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;

import com.blunka.framework.client.constants.DesignConstant;
import com.blunka.framework.client.constants.ImageConstant;
import com.blunka.framework.client.db.druid.DBSQLService;
import com.blunka.framework.client.enums.ModelStates;
import com.blunka.framework.client.enums.ModelType;
import com.blunka.framework.client.ui.entity.BlkTableMetaData;
import com.blunka.framework.client.ui.tools.ActionUtils;
import com.blunka.framework.client.utils.DesignUtils;

/**
 *
 *
 * @平台：统一开发平台
 * @文件名称：ViewSqlPanel.java
 * @文件描述：视图sql编辑器
 * @author xuqiang
 * @日期：2018年5月29日上午10:50:11
 *
 */
public class ViewSqlPanel extends JPanel{

	public static ViewSqlPanel viewSqlPanel = new ViewSqlPanel();

	/**
	 * Sql编辑器
	 */
	private RSyntaxTextArea viewSqlTextArea;

	/**
	 * 工具栏
	 */
	private JToolBar toolBar;

	public static ViewSqlPanel newInstance(){
		return viewSqlPanel;
	}

	private ViewSqlPanel(){
		this.setLayout(new BorderLayout());
		viewSqlTextArea = new RSyntaxTextArea();
		viewSqlTextArea.setSyntaxEditingStyle(RSyntaxTextArea.SYNTAX_STYLE_SQL);
		JScrollPane areaScrollpanel = new JScrollPane(this.viewSqlTextArea);
		add(areaScrollpanel, BorderLayout.CENTER);

		createToolBar();
		add(toolBar,BorderLayout.NORTH);
	}

	private void createToolBar(){
		toolBar = new JToolBar();
		JButton btn = new JButton("保存");
		btn.setIcon(new ImageIcon(ImageConstant.SYSTEM_IMG));
		btn.setToolTipText("保存");
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String viewSqlConfig = viewSqlTextArea.getText();
				if (viewSqlConfig.indexOf("\"") >= 0) {
					DesignUtils.showMessage(null, "输入的信息包含非法字符“双引号”！", "系统提示", JOptionPane.WARNING_MESSAGE);
					return;
				}
				//判断sql语句是否合法
				if(!DBSQLService.parseSql(viewSqlConfig)){
					DesignUtils.showMessage(null, "创建视图语句不合法，请确保在英文状态下输入的sql", "系统提示", JOptionPane.WARNING_MESSAGE);
					return;
				}
				ActionUtils.saveOrModifyViewSql(viewSqlConfig);

			}
		});
		toolBar.add(btn);
	}
	/**
	 * 设置视图sql语句
	 * @param sql
	 */
	public void setViewSql(String sql){
		viewSqlTextArea.setText(sql);
	}

	/**
	 * 获取视图sql语句
	 * @return
	 */
	public String getViewSql(){
		return viewSqlTextArea.getText();
	}
}
